﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Collections.Specialized;
using AlexandriaPressAuthorBusinessLayer;
using AlexandriaPressUserBusinessLayer;

namespace AlexandriaPressWebsite.MasterPages
{
    public partial class WebForm8 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BusinessAuthor bus = new BusinessAuthor();
            if (!Page.IsPostBack)
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    if (bus.validateUserIfAuthor(bus.getAuthorID(HttpContext.Current.User.Identity.Name)))
                    {
                        lblDraftTitleValidator.Visible = false;
                    }
                    else
                    {
                        Response.Redirect("~/MasterPages/UserPages/LoggedInUserPage.aspx");
                    }

                }
                else
                {
                    Response.Redirect("~/MasterPages/MainPage.aspx");
                }
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            BusinessAuthor business = new BusinessAuthor();
            UserBusinsessLayer user = new UserBusinsessLayer();


            lblFileUploadStatus.Text = business.submitDraft(txtBookTitle.Text, txtGenre.Text, 
                user.getUser(HttpContext.Current.User.Identity.Name).UserID, 
                fileUpload.FileName.Replace(" ", ""), 
                Server.MapPath(business.filePath() + fileUpload.FileName.Replace(" ", "")), 
                Server.MapPath(business.imagePath() + FileUpload1.FileName), 
                fileUpload.PostedFile, FileUpload1.PostedFile, FileUpload1.FileName,
                user.getUser(HttpContext.Current.User.Identity.Name).Email, 
                HttpContext.Current.User.Identity.Name, 
                business.checkDraftTitle(txtBookTitle.Text, 
                business.getAuthorID(HttpContext.Current.User.Identity.Name)));
        }

        protected void txtBookTitle_TextChanged(object sender, EventArgs e)
        {
            BusinessAuthor business = new BusinessAuthor();
            UserBusinsessLayer user = new UserBusinsessLayer();
            lblDraftTitleValidator.Visible = false;
            lblDraftTitleValidator.Text = business.checkDraftTitle(txtBookTitle.Text,business.getAuthorID(HttpContext.Current.User.Identity.Name));
            Label2.Text = business.drftVldtrIfExists(lblDraftTitleValidator.Text,txtBookTitle.Text);
        }
    }
}
